package com.ruoyi.web.controller.web3j;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web3jutil.Web3jUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import java.math.BigDecimal;

/**
 * 区块链交易控制器
 * 
 * @author ruoyi
 */
@Api(tags = "区块链交易管理")
@RestController
@RequestMapping("/web3j/transaction")
public class Web3jTransactionController {

    /**
     * 转账ETH
     * 
     * @param senderPrivateKey 发送者私钥
     * @param receiverAddress 接收者地址
     * @param amount 转账金额
     * @return 交易结果
     */
    @ApiOperation(value = "ETH转账", notes = "从一个账户向另一个账户转账ETH")
    @PostMapping("/transfer")
    public AjaxResult transferEther(
                                   @ApiParam(value = "发送者私钥", required = true) @RequestParam("senderPrivateKey") String senderPrivateKey,
                                   @ApiParam(value = "接收者地址", required = true) @RequestParam("receiverAddress") String receiverAddress,
                                   @ApiParam(value = "转账金额", required = true) @RequestParam("amount") BigDecimal amount) {
        try {
            TransactionReceipt receipt = Web3jUtil.transfer(senderPrivateKey, receiverAddress, amount, "ETH");
            return AjaxResult.success("转账成功", new TransactionInfo(
                    receipt.getTransactionHash(),
                    receipt.getBlockHash(),
                    receipt.getBlockNumber().toString(),
                    receipt.getGasUsed().toString(),
                    receipt.getStatus()
            ));
        } catch (Exception e) {
            return AjaxResult.error("转账失败: " + e.getMessage());
        }
    }

    /**
     * 交易信息内部类
     */
    static class TransactionInfo {
        private String transactionHash;
        private String blockHash;
        private String blockNumber;
        private String gasUsed;
        private String status;

        public TransactionInfo(String transactionHash, String blockHash, String blockNumber, String gasUsed, String status) {
            this.transactionHash = transactionHash;
            this.blockHash = blockHash;
            this.blockNumber = blockNumber;
            this.gasUsed = gasUsed;
            this.status = status;
        }

        public String getTransactionHash() {
            return transactionHash;
        }

        public void setTransactionHash(String transactionHash) {
            this.transactionHash = transactionHash;
        }

        public String getBlockHash() {
            return blockHash;
        }

        public void setBlockHash(String blockHash) {
            this.blockHash = blockHash;
        }

        public String getBlockNumber() {
            return blockNumber;
        }

        public void setBlockNumber(String blockNumber) {
            this.blockNumber = blockNumber;
        }

        public String getGasUsed() {
            return gasUsed;
        }

        public void setGasUsed(String gasUsed) {
            this.gasUsed = gasUsed;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }
}